import { queryJobList, queryPieData, queryPieData2 } from "../dao/pageDao3.js";

const path = new Map();

function getPieData1(req, res) {
    console.log('调用了 getPieData1');
    queryPieData(function (result) {
        res.writeHead(200);
        var nameList = []
        var dataModel = {};
        var total = 0;
        result.forEach(element => {
            let value = parseInt(element.Total);
            if (!nameList.some(item => item === element.Type)) {
                nameList.push(element.Type);
            }
            if (element.Type in dataModel) {
                dataModel[element.Type] += value
            } else {
                dataModel[element.Type] = value
            }
            total += parseInt(value);
        });
        let data = []
        for (let attr in dataModel) {
            data.push({
                name: attr,
                value: dataModel[attr]
            })
        }

        let resultJson = {
            nameList: nameList,
            data: data,
            total: total
        };

        res.write(JSON.stringify(resultJson));
        res.end();
    });
}

function getPieData2(req, res) {
    console.log('调用了 getPieData2');
    queryPieData2(function (result) {
        res.writeHead(200);
        var nameList = []
        var dataModel = {};
        var total = 0;
        result.forEach(element => {
            let value = parseInt(element.Total);
            if (!nameList.some(item => item === element.Level)) {
                nameList.push(element.Level);
            }
            if (element.Level in dataModel) {
                dataModel[element.Level] += value
            } else {
                dataModel[element.Level] = value
            }
            total += parseInt(value);
        });
        let data = []
        for (let attr in dataModel) {
            data.push({
                name: attr,
                value: dataModel[attr]
            })
        }
        let resultJson = {
            nameList: nameList,
            data: data,
            total: total
        };

        res.write(JSON.stringify(resultJson));
        res.end();
    });
}


function getJobList(req, res) {
    console.log('调用了 getJobList');

    queryJobList(function (result) {
        res.writeHead(200);
        let data = ''
        result.forEach(element =>{
            data += "<li>" + element.device_name + element.precinct_name + '工单告警<span class="zm">正面</span></li>';
        })

        let resultJson = {
            data: data,
        };

        res.write(JSON.stringify(resultJson));
        res.end();
    });
}


path.set('/getPieData1', getPieData1);
path.set('/getPieData2', getPieData2);
path.set('/getJobList', getJobList);


export { path };
